package com.wkr.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.dianping.cat.Cat;
import com.wkr.common.AppException;
import com.wkr.common.CommonResponse;
import com.wkr.order.feignclient.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
@RequestMapping("/route")
public class RouteController {

    @Value("${what.your.name}")
    String name;

    @Value("${server.port}")
    String serverPort;
    @Autowired
    private OrderFeignClient orderFeignClient;

    @SentinelResource(value = "purchase", fallback = "purchaseFallBack", exceptionsToIgnore = AppException.class)
    @GetMapping("/hi")
    public String request() {
        System.out.println(orderFeignClient.hi());
        Cat.logMetricForCount("testCount", 1);
        try {
            int x = 1 / 0;
        } catch (Exception e) {
            Cat.logError("异常", e);
        }
        return "请求到了端口号为:【" + serverPort + "】服务";
    }
    @GetMapping("/hi2")
    public String request2() {
//        System.out.println(orderFeignClient.createTrainOrder());
//        return "请求到了端口号为:【" + serverPort + "】服务";
        return name;
    }

    @GetMapping("/test")
    public String testForTimeOut() {
        CommonResponse<Boolean> res = orderFeignClient.testForTimeOut();
        return JSONObject.toJSONString(res);
    }
}
